home *** CD-ROM | disk | FTP | other *** search
/ IRIX Installation Tools & Overlays 2002 November / SGI IRIX Installation Tools & Overlays 2002 November - Disc 4.iso / dist / infosearch.idb / usr / lib / infosearch / bin / manTOC.z / manTOC
Text File  |  2002-10-15  |  14KB  |  582 lines

  1. #!/usr/bin/perl
  2.  
  3. require 5;
  4.  
  5. $bgcolor    = "#f9f9f9";
  6. $doctype    = "<!DOCTYPE HTML PUBLIC" .
  7.               "\"-//W3C//DTD HTML 3.2//EN\">\n";
  8. $comment    = "<!-- Generated by infosearch manTOC -->\n";
  9. $bHTML      = "<HTML>\n";
  10. $eHTML      = "</HTML>\n";
  11. $bTitle     = "<HEAD>\n    <TITLE>";
  12. $eTitle     = "</TITLE>\n</HEAD>\n";
  13. $bBody      = "<BODY BGCOLOR=\"$bgcolor\">";
  14. $eBody      = "</BODY>\n";
  15. $cgiBase    = "<A HREF=\"/cgi-bin/infosrch.cgi?cmd=getdoc&db=man&fname=";
  16. $cgiBrowseBase = "<A HREF=\"/cgi-bin/infosrch.cgi?cmd=browse&db=man&fname=";
  17. $cgiMan     = " <A HREF=\"/cgi-bin/infosrch.cgi?cmd=browse&db=man\">";
  18. $bBQ        = "<P>";
  19. $eBQ        = "</P>";
  20.  
  21. $makewhatis = "/usr/share/catman/whatis";
  22. $sgiIndex   = "/usr/share/catman/SGIindex";
  23. $fmtwidth   = 23;
  24. $tdebug     = 0;
  25.  
  26. %sectionCount = ();
  27.  
  28. %sectionNames = (
  29.                   "0" => "Man Pages",
  30.           "1" => "User Commands (1)",
  31.           "2" => "System Calls (2)",
  32.           "3" => "Subroutines (3)",
  33.           "4" => "File Formats (4)",
  34.           "5" => "Miscellaneous (5)",
  35.           "6" => "Games and Demos (6)",
  36.           "7" => "Special Files (7)",
  37.           "8" => "System Administration (8)",
  38.                   "D" => "Device Driver (D)"
  39. );
  40.  
  41. $arg1 = $ARGV[0];
  42.  
  43. # If -whatis, then also output the man title data to the whatis database
  44.  
  45. if($ARGV[1] eq "-whatis"){
  46.     $whatis = 1;
  47. }else{
  48.     $whatis = 0;
  49. }
  50.  
  51. # if -coll then add "&coll=" to the getdoc URL
  52. if ($ARGV[1] eq "-coll" && $cgiBase =~ /(.*)&db=man(.*)/) {
  53.    $cgiBase = $1 . "&coll=" . $ARGV[2] . "&db=man" . $2;
  54. }
  55.  
  56. # if -coll then add "&coll=" to the browse URL
  57. if ($ARGV[1] eq "-coll" && $cgiBrowseBase =~ /(.*)&db=man(.*)/) {
  58.    $cgiBrowseBase = $1 . "&coll=" . $ARGV[2] . "&db=man" . $2;
  59. }
  60.  
  61. if ($arg1 eq ""){
  62.     # Build from default title file location
  63.     &buildFTIndexList("${sgiIndex}/man.ttl");
  64. }else{
  65.     if($arg1=~/\.ttl/){
  66.     # Build from a title file
  67.     &buildFTIndexList($arg1,$whatis);
  68.     }else{ 
  69.         # Build from makewhatis database (called from CGI)
  70.     &buildMakeWhatIndex($makewhatis,$arg1);
  71.     }
  72. }
  73.  
  74.  
  75. #######################################################################
  76. #
  77. # buildFTIndexList(<index title file>) 
  78. #
  79. #######################################################################
  80.  
  81.  
  82. sub buildFTIndexList{
  83.  
  84.     local($ttl) = shift @_;
  85.     local($whatis) = shift @_;
  86.     local($inBQ) = 0;
  87.     local($lastAlpha) = "";
  88.     local($thisAlpha) = "";
  89.  
  90.     if ( -r $ttl ){
  91.     $titlefiles = $ttl;
  92.     }else{
  93.     die "Unable to read $ttl\n";
  94.     }
  95.  
  96.     $tocPath =  $ttl;
  97.     $tocPath =~ s/(SGIindex)\/.+/$1/;
  98.  
  99.     $localttl = $ttl;
  100.     $localttl =~ s/([^\/]*)\.ttl$/local$1\.ttl/;
  101.     
  102.     if ( -e $localttl ){ # Only if the local file exists (else strings error)
  103.     $titlefiles = "$titlefiles $localttl"; 
  104.     }
  105.     
  106.     open(TITLESTR, "strings $titlefiles |" ) 
  107.     || die "Error: Can't open $ttl\n";
  108.  
  109.     while(<TITLESTR>){       # Build a list of all the files in the index  
  110.     
  111.     if (/^F(.+)\n/){ # File Line
  112.         # Take the filename exactly as it appears in the index
  113.             # title file (.ttl file).
  114.         $filename=$1;
  115.     }elsif(/^t(.+)\n/){  # Title Line
  116.         $title = $1;
  117.         $filename=~s/\+/%2b/g; # Files with + in the name break the cgi
  118.  
  119.             if($title =~/\((\d)/){         # Get Section from title
  120.                 $section = $1;
  121.             }elsif($filename =~/cat(\d)/){ # Guess Section from filename
  122.                 $section = $1;
  123.  
  124.             } elsif ($filename =~ /catD/) {
  125.                 $section = "D";
  126.             }else{
  127.                 $section =  5;
  128.             }
  129.  
  130.             if ($section ne "D" &&
  131.                ($section > 8 || $section < 1)) { # Fix out of range sections
  132.                 $section=5; # Misc Section
  133.             }
  134.  
  135.         push(@{$section}, "$title\t$filename");
  136.         $sectionCount{$section}++;
  137.     } 
  138.     }
  139.     close(TITLESTR);
  140.  
  141.     # Remove existing index files
  142.     system("rm -f $tocPath/cat*.html $tocPath/man.html");
  143.     die "rm failed" if ($?);
  144.  
  145.     &initTOCFiles($tocPath);
  146.  
  147.     # Bug cat4.html /dev/fs is not correctly indented
  148.     # if we start at $i=4, it works
  149.  
  150.      if($whatis){  
  151.         open(WHATIS, "|sort -f -u > $makewhatis")
  152.         || die "Error: Can't open $makewhatis";
  153.      }
  154.  
  155.     foreach $i (keys %tocFiles) {
  156.     
  157.     open($tocFiles{$i}, ">> $tocFiles{$i}") 
  158.         || die "Can't open section file\n";
  159.     
  160.     &HTMLFileHeader($tocFiles{$i}, $sectionNames{$i}, "$i");
  161.         &alphaHeader($tocFiles{$i});
  162.     
  163.     @{$i} = sort sortCaseInsensitive @{$i};
  164.     
  165.     $inBQ=0;
  166.     $lastAlpha = "";
  167.     $thisAlpha = "";
  168.  
  169.     # Print each title to the correct file
  170.  
  171.     foreach (@{$i}){           
  172.  
  173.         if(/([^\t]*)\t(.*)/){
  174.         $title = $1;
  175.         $whatistitle = $title;
  176.         $filename = $2;
  177.         $title=&formatTitle($title);
  178.  
  179.         $thisAlpha=&setAlpha($title);
  180.         &alphaIndexIfNeeded($inBQ,$tocFiles{$i},$lastAlpha,$thisAlpha);
  181.         $lastAlpha = $thisAlpha;
  182.  
  183.         if($inBQ == 0){ # Correct indentation
  184.             $indent = $bBQ;
  185.             $inBQ = 1;
  186.         }else{
  187.             $indent = "";
  188.         }
  189.  
  190.         if($whatis){  # Also output to whatis man database
  191.                   # in the exact same format as makewhatis
  192.             $j = 0;
  193.             $k = 0;
  194.                        
  195.             $whatistitle =~s/\ +$//; # Remove trailing spaces
  196.             
  197.             # Debug for man page titles that we can't read.
  198.             
  199.             if($whatistitle =~/\[No\ title\]/){
  200.                 if($tdebug == 1){
  201.                 $whatistitle = "[No title] $filename";
  202.             }else{
  203.                 $whatistitle = ""; # Skip [No title]
  204.             }
  205.             }
  206.            
  207.             while(substr($whatistitle,$j,$j+1)){
  208.     
  209.             if((substr($whatistitle,$j,1) eq "-") && ($j > 0) &&
  210.                (substr($whatistitle,$j-1,1) eq " ") && 
  211.                ($j <= $fmtwidth)){
  212.                
  213.                 $k = $fmtwidth-$j+1;
  214.                 while($k > 0){
  215.                 print WHATIS " "
  216.                   or die "print: $!";
  217.                 $k--;
  218.                 }
  219.                 print WHATIS substr($whatistitle,$j,1024)
  220.                   or die "print: $!";
  221.                 last;
  222.             }
  223.             print WHATIS substr($whatistitle,$j,1)
  224.               or die "print: $!";
  225.             $j++;
  226.             }
  227.             print WHATIS "\n"
  228.               or die "print: $!";
  229.             }
  230.             
  231.         print {$tocFiles{$i}} "  ",
  232.                         "${cgiBase}${filename}\">${title}<BR>\n"
  233.           or die "print: $!";
  234.  
  235.         }
  236.     
  237.         }
  238.     if($inBQ == 1){
  239.         print {$tocFiles{$i}} "$eBQ\n"
  240.               or die "print: $!";
  241.         $inBQ=0;
  242.     }
  243.     &HTMLFileTrailer($tocFiles{$i});
  244.     }
  245.  
  246.  
  247.     open($tocFiles{'0'}, "> $tocFiles{'0'}") 
  248.     || die "Can't open section file\n";
  249.     &HTMLFileHeader($tocFiles{'0'}, $sectionNames{0}, '0');
  250.     &outputTopIndex($tocFiles{'0'});
  251.     &HTMLFileTrailer($tocFiles{'0'});
  252.     close($tocFiles{'0'}) or die "close tocfile";
  253.     
  254.     $manmsg   = "updated man page whatis database";
  255.     system("logger -t sgindexAdmin \"$manmsg\"");
  256.     die "logger failed" if ($?);
  257. }
  258.  
  259. #######################################################################
  260. #
  261. # buildMakeWhatIndex(<makewhatis,sectionNumber>) 
  262. #
  263. #######################################################################
  264.  
  265. sub buildMakeWhatIndex{
  266.  
  267.     local($makewhat,$targetString) = @_;
  268.     local($inBQ) = 0;
  269.     local($lastAlpha) = "";
  270.     local($thisAlpha) = "";
  271.  
  272.     if($targetString=~/cat([0-8])/){
  273.     # Output index for this $targetSection only
  274.     $targetSection = $1;
  275.     }elsif($targetString=~/man/){
  276.     $targetSection = 0; # Top level man index
  277.     }else{
  278.     die "invalid arg: $targetString - use: man or cat[1-8]";
  279.     }
  280.  
  281.     open(TITLESTR,$makewhat) ||
  282.     die "can't open $makewhat";
  283.  
  284.     &HTMLFileHeader(STDOUT,$sectionNames{$targetSection}, $targetSection);
  285.  
  286.     if($targetSection != 0){
  287.     &alphaHeader(STDOUT);
  288.     }
  289.  
  290.     while(<TITLESTR>){ # Build a list of all the files in the index  
  291.     
  292.     if (/(\S+)\ *\((\d)[^)]{0,3}\)/){
  293.     
  294.         $cmd    = $1;
  295.         $section    = $2;
  296.         $title    = $_;
  297.         
  298.         if($section > 8 || $section < 1){ # Correct bogus section numbers
  299.         $section=5;                   # Misc Section
  300.         }
  301.         $sectionCount{$section}++;
  302.  
  303.         $title=&formatTitle($title);
  304.         $thisAlpha=&setAlpha($title);
  305.         if($targetSection != 0){
  306.         &alphaIndexIfNeeded($inBQ,STDOUT,$lastAlpha,$thisAlpha);
  307.         $lastAlpha = $thisAlpha;
  308.         }
  309.  
  310.         if($section == $targetSection){
  311.         if($inBQ == 0){
  312.             $indent = $bBQ;
  313.             $inBQ = 1;
  314.         }else{
  315.             $indent = "";
  316.         }
  317.         print "${indent}",
  318.         "${cgiBase}${section}%20${cmd}\">${title}<BR>\n"
  319.           or die "print: $!";
  320.         }  
  321.     }    
  322.     } 
  323.     
  324.     close(TITLESTR);
  325.  
  326.     if($targetSection == 0){
  327.         &outputTopIndex(STDOUT);
  328.     }
  329.  
  330.     if($inBQ == 1){
  331.     print "$eBQ\n"
  332.       or die "print: $!";
  333.     }
  334.  
  335.     &HTMLFileTrailer(STDOUT);
  336.  
  337. }
  338.  
  339. #######################################################################
  340. #
  341. # outputTopIndex()
  342. #
  343. #######################################################################
  344.  
  345. sub outputTopIndex{
  346.  
  347.     local($fd) = @_;
  348.     local $icon = 
  349.     "<IMG SRC=\"/infosearch/images/folder.gif\" " .
  350.     "BORDER=\"0\" ALIGN=\"BOTTOM\" ALT=\"+\">";
  351.  
  352.     # Print the browse top level browse index
  353.  
  354.     print ${fd} "<p>\n" or die "print: $!";
  355.     foreach $i (sort keys %sectionCount) {
  356.     
  357.     if ($sectionCount{$i} != 0){
  358.         $linkdoc = sprintf("cat%s.html",$i);
  359.         print ${fd} "  ",
  360.                        "$cgiBrowseBase$linkdoc\">$icon<\/A>  ",
  361.                        "$cgiBrowseBase$linkdoc\">$sectionNames{$i}<\/A><BR>\n"
  362.            or die "print: $!";
  363.     }    
  364.     }
  365.     print ${fd} "</p>\n" or die "print: $!";
  366. }
  367.  
  368. #######################################################################
  369. #
  370. # replaceHTMLEntities(string)
  371. #
  372. #######################################################################
  373.  
  374. sub replaceHTMLEntities{
  375.  
  376.     local($str) = @_;
  377.  
  378.     $str =~ s/</</g;
  379.     $str =~ s/>/>/g;
  380.  
  381.     return $str;
  382.  
  383. }
  384.  
  385. #######################################################################
  386. #
  387. # HTMLFileHeader(fd, title, targetid)
  388. #
  389. #######################################################################
  390.  
  391. sub HTMLFileHeader{
  392.  
  393.     local($filehandle,$title,$target)= @_;
  394.     
  395.     print { $filehandle } 
  396.     "$doctype$comment$bHTML$bTitle$title$eTitle$bBody\n"
  397.       or die "print: $!";
  398.  
  399.     my($db_ttl) = $sectionNames{'0'};
  400.  
  401.     print { $filehandle } 
  402.     "<h3>", ($target ne '0' ? "${cgiMan}${db_ttl}</a> : " : ''),
  403.     "${title}</H3>\n"
  404.       or die "print: $!";
  405.  
  406. }
  407.  
  408.  
  409. #######################################################################
  410. #
  411. # HTMLFileTrailer(fd)
  412. #
  413. #######################################################################
  414.  
  415. sub HTMLFileTrailer{
  416.  
  417.     local($filehandle) = @_;
  418.     
  419.     print { $filehandle }  "$eBody$eHTML"
  420.       or die "print: $!";
  421.  
  422. }
  423.  
  424. #######################################################################
  425. #
  426. # initTOCFiles(tocPath)
  427. #
  428. #######################################################################
  429.  
  430. sub initTOCFiles{
  431.  
  432.     local($tocPath) = @_;
  433.  
  434.     %tocFiles = (
  435.        '0' => "$tocPath/man.html",
  436.        '1' => "$tocPath/cat1.html",
  437.        '2' => "$tocPath/cat2.html",
  438.        '3' => "$tocPath/cat3.html",
  439.        '4' => "$tocPath/cat4.html",
  440.        '5' => "$tocPath/cat5.html",
  441.        '6' => "$tocPath/cat6.html",
  442.        '7' => "$tocPath/cat7.html",
  443.        '8' => "$tocPath/cat8.html",
  444.        'D' => "$tocPath/catD.html");
  445.  
  446. }
  447.  
  448. #######################################################################
  449. #
  450. # formatTitle(titleString)
  451. #
  452. #######################################################################
  453.  
  454. sub formatTitle{
  455.  
  456.     local($title) = @_;
  457.  
  458.     $title = &replaceHTMLEntities($title);  
  459.     $title =~ s/\ +/\ /g;    # Remove duplicate spaces
  460.     $title =~ s/\n//;        # Remove any \n
  461.     unless($title=~ s/\)/\)<\/A>/){ # Insert </A>
  462.     unless($title=~s/-/<\/A>-/){
  463.         $title=~s/$/<\/A>/;
  464.     }
  465.     }
  466.     return $title;
  467.  
  468. }
  469.  
  470. #######################################################################
  471. #
  472. # setAlpha(titleString)
  473. #
  474. #######################################################################
  475.  
  476. sub setAlpha{
  477.  
  478.     local($title) = @_;
  479.     local($alpha) = "";
  480.  
  481.     if($title=~/^([a-zA-Z])/){
  482.     $alpha=$1;
  483.     $alpha=~tr/a-z/A-Z/;
  484.     }
  485.     
  486.     return $alpha;
  487.  
  488. }
  489.  
  490.  
  491. #######################################################################
  492. #
  493. # alphaHeader()
  494. #
  495. #######################################################################
  496.  
  497. sub alphaHeader{
  498.  
  499.     local($filehandle) = @_;
  500.     
  501.     local(@alphabet) = 
  502.     (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
  503.  
  504.     print {$filehandle} "<P><center><B>\n"
  505.       or die "print: $!";
  506.  
  507.     foreach(@alphabet){
  508.     print {$filehandle} "<A HREF=\"#$_\">$_</A> \n"
  509.       or die "print: $!";
  510.     }
  511.  
  512.     print {$filehandle} "</B></center></P>\n"
  513.       or die "print: $!";
  514. }
  515.  
  516. #######################################################################
  517. #
  518. # alphaIndex($inBQ,$fd,$lastAlpha,$thisAlpha)
  519. #
  520. #######################################################################
  521.  
  522. sub alphaIndexIfNeeded{
  523.  
  524.     local($fd);
  525.     ($inBQ, $fd, $lastAlpha, $thisAlpha) = @_;
  526.  
  527.     local(@alphabet) = 
  528.     (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z);
  529.     
  530.     if($thisAlpha ne $lastAlpha && $thisAlpha ne ""){
  531.     if($inBQ == 1){
  532.         $exdent = "$eBQ\n";
  533.         $inBQ = 0;
  534.     }else{
  535.         $exdent = "";
  536.     }
  537.     
  538.     $off = (ord($thisAlpha)-ord($lastAlpha));
  539.  
  540.     print {$fd} $exdent;
  541.  
  542.     if($off > 1 && $lastAlpha ne ""){
  543.         local($a) = 0;
  544.         while($lastAlpha ne $alphabet[$a]){
  545.         $a++;
  546.         }
  547.         $a = $a+1;
  548.         for($j=0;$j<$off;$j++){
  549.         print {$fd} "<H3><A NAME=\"$alphabet[$j+$a]\">" .
  550.             "$alphabet[$j+$a]</A></H3>\n"
  551.               or die "print: $!";
  552.         }
  553.     }else{
  554.         print {$fd} "<H3><A NAME=\"$thisAlpha\">" .
  555.         "$thisAlpha</A></H3>\n"
  556.           or die "print: $!";
  557.     }
  558.     }
  559.  
  560.     
  561.     $lastAlpha = $thisAlpha;
  562.     
  563. }
  564.  
  565. #######################################################################
  566. #
  567. # sortCaseInsensitive
  568. #
  569. #######################################################################
  570.  
  571. sub sortCaseInsensitive
  572. {           
  573.     local($tmp_a) = $a;
  574.     local($tmp_b) = $b;
  575.  
  576.     $tmp_a =~ tr/A-Z/a-z/;
  577.     $tmp_b =~ tr/A-Z/a-z/;
  578.     
  579.     return($tmp_a cmp $tmp_b);
  580. }
  581.  
  582.